﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace File_Operate_Program
{
    public partial class FrmMain : Form
    {
        IniFiles ini;
        static Singleton singleton = Singleton._instance;

        public FrmMain()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            string sSaveData = string.Empty;
            singleton.FilePath = System.Environment.CurrentDirectory;
            singleton.ConfigIniPath = singleton.FilePath + @"\config.ini";

            #region 创建Ini
            ini = new IniFiles(singleton.ConfigIniPath);
            bool isExists = ini.ExistINIFile();
            if (!isExists)
            {
                FileStream IniFile = new FileStream(singleton.ConfigIniPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            }
            #endregion

            #region Ini内容初始化
            CSV_Ini_Init();
            #endregion

            Encoding DataEncoding = Encoding.Default;
            #region gb2312
            string 测试FilePath = singleton.FilePath + "\\gb2312测试.csv";
            if (!File.Exists(测试FilePath))
            {
                sSaveData = "//注释测试\n//注释测试\n//注释测试\nid,EncodingType,value\n" + "1,gb2312,测试1\n" + "2,gb2312,测试2\n" + "3,gb2312,测试3";
            }
            else
            {
                sSaveData = string.Empty;
            }

            //DataEncoding = Encoding.GetEncoding(936); 
            DataEncoding = Encoding.GetEncoding("GB2312");
            if(!string.IsNullOrEmpty(sSaveData))
            {
                CCSV_Operate.SaveCSV(测试FilePath, sSaveData, DataEncoding); 
            }            
            #endregion

            #region utf-8-bom
            测试FilePath = singleton.FilePath + "\\utf-8-bom测试.csv";
            if (!File.Exists(测试FilePath))
            {
                sSaveData = "//注释测试\n//注释测试\n//注释测试\nid,EncodingType,value\n" + "1,utf-8-bom,测试1\n" + "2,utf-8-bom,测试2\n" + "3,utf-8-bom,测试3";
            }
            else
            {
                sSaveData = string.Empty;
            }

            //DataEncoding = Encoding.GetEncoding(65001);
            DataEncoding = new UTF8Encoding(true);
            if (!string.IsNullOrEmpty(sSaveData))
            {
                CCSV_Operate.SaveCSV(测试FilePath, sSaveData, DataEncoding);
            }
            #endregion

            #region utf-8
            测试FilePath = singleton.FilePath + "\\utf-8测试.csv";
            if (!File.Exists(测试FilePath))
            {
                sSaveData = "//注释测试\n//注释测试\n//注释测试\nid,EncodingType,value\n" + "1,utf-8,测试1\n" + "2,utf-8,测试2\n" + "3,utf-8,测试3";
            }
            else
            {
                sSaveData = string.Empty;
            }

            DataEncoding = new UTF8Encoding(false);
            if (!string.IsNullOrEmpty(sSaveData))
            {
                CCSV_Operate.SaveCSV(测试FilePath, sSaveData, DataEncoding);
            }
            #endregion

            #region 方法赋值
            this.BtnCheckEncodingType.Click += new System.EventHandler(this.BtnCheckEncodingType_Click);
            this.BtnCSV_Editor.Click += new System.EventHandler(this.BtnCSV_Editor_Click);
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmMain_FormClosed);
            this.BtnIni_Editor.Click += new System.EventHandler(this.BtnIni_Editor_Click);
            this.BtnJson_Editor.Click += new System.EventHandler(this.BtnJson_Editor_Click);
            #endregion

            #region 程序功能说明
            this.RTxtTips.ReadOnly = true;
            this.RTxtTips.AppendText($"以下是本程序功能说明：\n");
            this.RTxtTips.AppendText($"检测文件编码格式按钮：会打开检测文件的新页面，在新页面中可以通过选择文件按钮选择可选格式的文件，然后文件的各项数据会在下方的文本框处显示。\n");
            this.RTxtTips.AppendText($"CSV文件编辑器按钮：会打开CSV文件操作的新页面，具体说明请查看《使用说明文档.docx》。");
            #endregion

            #region 多线程测试
            ChangeBtnCSV_EditorFontColor();
            #endregion
        }

        private void BtnCheckEncodingType_Click(object sender, EventArgs e)
        {
            FrmCheckEncodingType frmCheckEncodingType = new FrmCheckEncodingType();
            frmCheckEncodingType.Show();
        }

        private void BtnCSV_Editor_Click(object sender, EventArgs e)
        {
            FrmCSV_Editor frmCSV_Editor = new FrmCSV_Editor();
            frmCSV_Editor.Show();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(Process.GetCurrentProcess().Id);
            process.Kill();
        }

        private void BtnIni_Editor_Click(object sender, EventArgs e)
        {
            MessageBox.Show("本功能待后续开发完善！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BtnJson_Editor_Click(object sender, EventArgs e)
        {
            MessageBox.Show("本功能待后续开发完善！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ChangeBtnCSV_EditorFontColor()
        {
            Task task = new Task(() =>
            {
                //*****以下是异步执行的代码*****
                bool IsLoop = true;
                int index = 0;
                while (IsLoop)
                {
                    Thread.Sleep(1000);//模拟耗时操作
                    MethodInvoker mi = new MethodInvoker(() =>
                    {
                        switch (index)
                        {
                            case 0:
                                BtnCSV_Editor.ForeColor = ColorTranslator.FromHtml("#FF0000");
                                index = index + 1;
                                break;
                            case 1:
                                BtnCSV_Editor.ForeColor = ColorTranslator.FromHtml("#FF7D00");
                                index = index + 1;
                                break;
                            case 2:
                                BtnCSV_Editor.ForeColor = ColorTranslator.FromHtml("#FFFF00");
                                index = index + 1;
                                break;
                            case 3:
                                BtnCSV_Editor.ForeColor = ColorTranslator.FromHtml("#00FF00");
                                index = index + 1;
                                break;
                            case 4:
                                BtnCSV_Editor.ForeColor = ColorTranslator.FromHtml("#00FFFF");
                                index = index + 1;
                                break;
                            case 5:
                                BtnCSV_Editor.ForeColor = ColorTranslator.FromHtml("#0000FF");
                                index = index + 1;
                                break;
                            case 6:
                                BtnCSV_Editor.ForeColor = ColorTranslator.FromHtml("#FF00FF");
                                index = 0;
                                break;
                        }
                    });
                    this.BeginInvoke(mi);
                }
                //*****以上是异步执行的代码*****
            });
            task.Start();
        }

        private void CSV_Ini_Init()
        {
            string sCBBDataEncoding = ini.IniReadValue("FrmCSV_Editor", "sCBBDataEncoding");
            if (string.IsNullOrEmpty(sCBBDataEncoding))
            {
                ini.IniWriteValue("FrmCSV_Editor", "sCBBDataEncoding", "UTF-8-BOM,UTF-8,GB2312");
            }

            string sOpenFileInitlDir = ini.IniReadValue("FrmCSV_Editor", "sOpenFileInitlDir");
            if (string.IsNullOrEmpty(sOpenFileInitlDir))
            {
                ini.IniWriteValue("FrmCSV_Editor", "sOpenFileInitlDir", singleton.FilePath);
            }

            string sFirstPlaceSpecialCharacters = ini.IniReadValue("FrmCSV_Editor", "sFirstPlaceSpecialCharacters");
            if (string.IsNullOrEmpty(sFirstPlaceSpecialCharacters))
            {
                ini.IniWriteValue("FrmCSV_Editor", "sFirstPlaceSpecialCharacters", "/");
            }
        }
    }
}
